package com.syni.mdd.yhd.common.all.utils;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.entity.BmsDxClientRole;
import com.syni.mdd.yhd.common.all.entity.SystemUser;
import com.syni.mdd.yhd.common.all.entity.user.BmsUser;
import com.syni.mdd.yhd.common.all.service.BmsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @className OpenIdTokenManager
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/12 11:06
 * @VERSION 1.0
 **/
@Component
public class TokenManager {
    public static RedisTemplate redisTemplate;

    private static BmsUserService bmsUserService;

    public static final String redisKeyByOpenId = Constants.REDIS_KEY_PREFIX + "openId:";

    public static final String redisKeyByUserId = Constants.REDIS_KEY_PREFIX + "userId:";

    @Autowired
    public void setBmsUserService(BmsUserService bmsUserService) {
        TokenManager.bmsUserService = bmsUserService;
    }

    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        TokenManager.redisTemplate = redisTemplate;
    }

    public static Integer getUserId(String openId){
        BmsUser bmsUser = getUser(openId);
        if(bmsUser != null){
            return bmsUser.getId();
        }
        return null;
    }

    public static BmsUser getUser(String openId){
        BmsUser bmsUser;
        String redisKeyTemp = redisKeyByOpenId + openId;
        if(redisTemplate.hasKey(redisKeyTemp)){
            bmsUser = (BmsUser) redisTemplate.opsForValue().get(redisKeyTemp);
        }else {
            bmsUser = bmsUserService.findUserByOpenId(openId);
            if(bmsUser != null) {
                redisTemplate.opsForValue().set(redisKeyTemp, bmsUser, 1, TimeUnit.DAYS);
            }
        }
        return bmsUser;
    }

    public static BmsUser getUser(Integer userId){
        if(userId == null){
            return null;
        }
        BmsUser bmsUser;
        String redisKeyTemp = redisKeyByUserId + userId;
        if(redisTemplate.hasKey(redisKeyTemp)){
            bmsUser = (BmsUser) redisTemplate.opsForValue().get(redisKeyTemp);
        }else {
            bmsUser = bmsUserService.findUserByUserId(userId);
            redisTemplate.opsForValue().set(redisKeyTemp, bmsUser, 1, TimeUnit.DAYS);
        }
        return bmsUser;
    }

    public static BmsUser flushUser(Integer userid){
        String redisKeyTemp = redisKeyByUserId + userid;
        BmsUser bmsUser = bmsUserService.findUserByUserId(userid);
        redisTemplate.opsForValue().set(redisKeyTemp, bmsUser, 1, TimeUnit.DAYS);
        return bmsUser;
    }

    public static BmsUser setUser(BmsUser bmsUser,String redisKey,String identifyId){
        try {
            if(bmsUser != null){
                bmsUserService.getBaseDao().saveAndFlush(bmsUser);
                if(StringUtils.isNotBlank(bmsUser.getAccountId())) {
                    SystemUser systemUser = bmsUserService.findSystemUser(bmsUser.getAccountId());
                    bmsUser.setSystemUser(systemUser);
                }
                //更新到redis
                redisTemplate.opsForValue().set(redisKey + identifyId,bmsUser,1, TimeUnit.DAYS);
            }
        }catch (Exception e){
            //不满足唯一约束
            if(redisKey.equals(redisKeyByOpenId)){
                BmsUser bmsUserOpen =bmsUserService.getBaseDao().findByOpenid(identifyId);
                if(bmsUserOpen != null){
                    bmsUser.setId(bmsUserOpen.getId());
                    bmsUser.setOpenid(bmsUserOpen.getOpenid());
                }
            }else {
                BmsUser bmsUserById = bmsUserService.getBaseDao().findById(Integer.parseInt(identifyId)).get();
                if(bmsUserById != null) {
                    bmsUser.setId(bmsUserById.getId());
                    bmsUser.setPhone(bmsUserById.getPhone());
                    bmsUser.setAccountId(bmsUserById.getAccountId());
                }
            }
            bmsUserService.getBaseDao().save(bmsUser);
            //更新到redis
            redisTemplate.opsForValue().set(redisKey + identifyId,bmsUser,1, TimeUnit.DAYS);
        }
        return bmsUser;
    }

    public static BmsUser setUser(BmsUser bmsUser){
        if(bmsUser != null){
            bmsUserService.getBaseDao().saveAndFlush(bmsUser);
            if(StringUtils.isNotBlank(bmsUser.getAccountId())) {
                SystemUser systemUser = bmsUserService.findSystemUser(bmsUser.getAccountId());
                bmsUser.setSystemUser(systemUser);
            }
            //更新到redis
            redisTemplate.opsForValue().set(redisKeyByUserId + bmsUser.getId().toString(),bmsUser,1, TimeUnit.DAYS);
        }
        return bmsUser;
    }
}
